<template>
  <div class="pay-page">
    <!-- 支付页面iframe -->
    <iframe
      ref="paymentIframe"
      :src="paymentUrl"
      class="payment-iframe"
      frameborder="0"
      allowfullscreen
      @load="onIframeLoad"
      @error="onIframeError"></iframe>

    <!-- 无支付地址状态 -->
    <!-- <div v-else class="no-url-container">
      <div class="no-url-icon">💳</div>
      <h2 class="no-url-title">未提供支付地址</h2>
      <p class="no-url-message">请提供有效的支付页面地址</p>
    </div> -->
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, onUnmounted } from 'vue';
  import { useRoute, useRouter } from 'vue-router';

  const route = useRoute();
  const router = useRouter();

  // 响应式数据
  const paymentUrl = ref<string>('');
  const loading = ref(true);
  const error = ref<string>('');
  const paymentIframe = ref<HTMLIFrameElement | null>(null);

  // 获取支付URL参数
  const getPaymentUrl = () => {
    // 从路由查询参数获取支付地址
    const url = route.query.url as string;
    const paymentUrlParam = route.query.payment_url as string;

    // 支持多种参数名
    const finalUrl = url || paymentUrlParam;

    if (finalUrl) {
      // 验证URL格式
      try {
        new URL(finalUrl);
        return finalUrl;
      } catch {
        error.value = '无效的支付地址格式';
        return null;
      }
    }

    error.value = '未提供支付地址参数';
    return null;
  };

  // iframe加载完成
  const onIframeLoad = () => {
    loading.value = false;
    error.value = '';
    console.log('支付页面加载完成');
  };

  // iframe加载错误
  const onIframeError = () => {
    loading.value = false;
    error.value = '支付页面加载失败，请检查网络连接或支付地址';
    console.error('支付页面加载失败');
  };

  // 重试加载
  const retryLoad = () => {
    loading.value = true;
    error.value = '';

    // 重新获取URL
    const url = getPaymentUrl();
    if (url) {
      paymentUrl.value = url;
    } else {
      loading.value = false;
    }
  };

  // 监听路由参数变化
  const handleRouteChange = () => {
    const url = getPaymentUrl();
    if (url) {
      paymentUrl.value = url;
      loading.value = true;
      error.value = '';
    }
  };

  // 页面挂载时初始化
  onMounted(() => {
    console.log('支付页面组件挂载');
    const url = getPaymentUrl();

    if (url) {
      paymentUrl.value = url;
      console.log('设置支付URL:', url);
    } else {
      loading.value = false;
    }
  });

  // 页面卸载时清理
  onUnmounted(() => {
    console.log('支付页面组件卸载');
  });
</script>

<style scoped lang="scss">
  .pay-page {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: #f5f5f5;
  }

  .loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: #fff;
  }

  .loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #f76b1b;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
  }

  @keyframes spin {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }

  .loading-text {
    color: #666;
    font-size: 16px;
    margin: 0;
  }

  .error-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: #fff;
    padding: 20px;
  }

  .error-icon {
    font-size: 48px;
    margin-bottom: 16px;
  }

  .error-title {
    color: #333;
    font-size: 24px;
    margin: 0 0 12px 0;
    font-weight: 600;
  }

  .error-message {
    color: #666;
    font-size: 16px;
    text-align: center;
    margin: 0 0 24px 0;
    line-height: 1.5;
  }

  .retry-btn {
    background: #f76b1b;
    color: white;
    border: none;
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: background-color 0.3s ease;

    &:hover {
      background: #e55a0f;
    }
  }

  .no-url-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: #fff;
    padding: 20px;
  }

  .no-url-icon {
    font-size: 48px;
    margin-bottom: 16px;
  }

  .no-url-title {
    color: #333;
    font-size: 24px;
    margin: 0 0 12px 0;
    font-weight: 600;
  }

  .no-url-message {
    color: #666;
    font-size: 16px;
    text-align: center;
    margin: 0;
    line-height: 1.5;
  }

  .payment-iframe {
    width: 100%;
    height: 100vh;
    border: none;
    background: #fff;
  }
</style>
